/*
 * InformationForm
 * 
 * Project: XCRM
 * 
 * Copyright 2012 by SunriseSoft
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of SunriseSoft. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license
 * agreements you entered into with SunriseSoft.
 */

package com.sunrise.xcrm.ui.detail;

import com.sunrise.xcrm.data.Information;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.Label;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

/**
 * @author Phan Hong Phuc
 * @since Aug 14, 2012
 */
@SuppressWarnings("serial")
public class InformationForm extends Form {
    private static final String EDIT = "Edit";
    private static final String OK = "OK";
    private static final String CANCEL = "Cancel";
    private CustomLayout layout;
    private Button editBtn;
    private Button cancelBtn;

    public InformationForm() {
        initLayout();
        setLayout(layout);

        // Set up buffering
        setWriteThrough(false); // we want explicit 'apply'
        setInvalidCommitted(false); // no invalid values in datamodel

        // FieldFactory for customizing the fields and adding validators
        setFormFieldFactory(new InformationFieldFactory());

        // Determines which properties are shown, and in which order:
        // setVisibleItemProperties(Arrays.asList(new String[] { "shortName", "provic" }));
        initFooter();

    }

    public InformationForm(BeanItem<Information> informationItem) {
        this();
        setItemDataSource(informationItem); // bind to POJO via BeanItem
        // TODO temp set the status of form is readonly and the edit button is visible
        // We need allow pass the status of form is readonly or not.
        setReadOnly(true);
    }

    public void initFooter() {
        cancelBtn = new Button(CANCEL, new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                event.getButton().setVisible(false);
                editBtn.setCaption(OK);
                discard();
                setReadOnly(true);
            }
        });
        cancelBtn.setVisible(false);

        editBtn = new Button(EDIT, new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                Button btn = event.getButton();
                if (EDIT.equals(btn.getCaption())) {
                    btn.setCaption(OK);
                    setReadOnly(false);
                    cancelBtn.setVisible(true);
                } else {
                    commit();
                    btn.setCaption(EDIT);
                    setReadOnly(true);
                    cancelBtn.setVisible(false);
                }
            }
        });

        getFooter().addComponent(editBtn);
        getFooter().addComponent(cancelBtn);
    }

    private void initLayout() {
        layout = new CustomLayout("InformationForm");
        layout.setWidth("100.0%");

        layout.addComponent(createLabel("Short name"), "shortNameLabel");
        layout.addComponent(createLabel("Name"), "nameLabel");
        layout.addComponent(createLabel("Province"), "provinceLabel");
        layout.addComponent(createLabel("Status"), "statusLabel");
        layout.addComponent(createLabel("Annual Revenue"), "annualLabel");
        layout.addComponent(createLabel("Fax"), "faxLabel");
        layout.addComponent(createLabel("Assigned To"), "assignedToLabel");
        layout.addComponent(createLabel("Contact Address"), "contactAddressLabel");
        layout.addComponent(createLabel("More Information"), "moreInformationLabel");
        layout.addComponent(createLabel("Employees"), "employeesLabel");
        layout.addComponent(createLabel("Office Phone"), "officePhoneLabel");
        layout.addComponent(createLabel("Office Phone 2"), "officePhone2Label");
        layout.addComponent(createLabel("Website"), "websiteLabel");
        layout.addComponent(createLabel("Member Of"), "memberOfLabel");
        layout.addComponent(createLabel("Invoice Address"), "invoiceAddressLabel");
    }

    private Label createLabel(String string) {
        Label label = new Label(string, Label.CONTENT_XHTML);
        label.setWidth("-1px");
        return label;
    }

    @Override
    protected void attachField(Object propertyId, Field field) {
        if (propertyId.equals("shortName")) {
            layout.addComponent(field, "shortName");
        } else if (propertyId.equals("province")) {
            layout.addComponent(field, "province");
        } else if (propertyId.equals("annualRevenual")) {
            layout.addComponent(field, "annualRevenual");
        } else if (propertyId.equals("officePhone1")) {
            layout.addComponent(field, "officePhone1");
        } else if (propertyId.equals("fax")) {
            layout.addComponent(field, "fax");
        } else if (propertyId.equals("assignedTo")) {
            layout.addComponent(field, "assignedTo");
        } else if (propertyId.equals("contactAddress")) {
            layout.addComponent(field, "contactAddress");
        } else if (propertyId.equals("moreInformation")) {
            layout.addComponent(field, "moreInformation");
        } else if (propertyId.equals("name")) {
            layout.addComponent(field, "name");
        } else if (propertyId.equals("status")) {
            layout.addComponent(field, "status");
        } else if (propertyId.equals("employees")) {
            layout.addComponent(field, "employees");
        } else if (propertyId.equals("officePhone2")) {
            layout.addComponent(field, "officePhone2");
        } else if (propertyId.equals("website")) {
            layout.addComponent(field, "website");
        } else if (propertyId.equals("memberOf")) {
            layout.addComponent(field, "memberOf");
        } else if (propertyId.equals("invoiceAddress")) {
            layout.addComponent(field, "invoiceAddress");
        }

    }

    private class InformationFieldFactory extends DefaultFieldFactory {

        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {
            Field f = super.createField(item, propertyId, uiContext);
            ((TextField) f).setNullRepresentation("");
            f.setWidth("100%");
            if ("name".equals(propertyId)) {
                ((TextField) f).setInputPrompt("Nhập tên đầy đủ của công ty");
            } else if ("province".equals(propertyId)) {
                f = createProviceComboBox();
            } else if ("status".equals(propertyId)) {
                f = createStatusComboBox();
            } else if ("assignedTo".equals(propertyId)) {
                // TODO
            } else if ("memberOf".equals(propertyId)) {
                // TODO
            } else if ("contactAddress".equals(propertyId)) {
                TextArea ta = new TextArea();
                ta.setNullRepresentation("");
                ta.setWidth("100%");
                f = ta;
            } else if ("invoiceAddress".equals(propertyId)) {
                TextArea ta = new TextArea();
                ta.setNullRepresentation("");
                ta.setWidth("100%");
                f = ta;
            } else if ("moreInformation".equals(propertyId)) {
                RichTextArea rta = new RichTextArea();
                rta.setNullRepresentation("");
                f = rta;
            }
            f.setCaption(null);
            return f;
        }

        private Field createStatusComboBox() {
            // TODO Auto-generated method stub
            ComboBox comboBox = new ComboBox();
            comboBox.setWidth("100%");
            return comboBox;
        }

        private Field createProviceComboBox() {
            ComboBox comboBox = new ComboBox();
            comboBox.addItem("Cần Thơ");
            comboBox.addItem("Hồ Chí Minh");
            comboBox.setWidth("100%");
            return comboBox;
        }

    }
}
